//
//  NSBundle+STSystemTool.m
//  TheKing
//
//  Created by Mac on 2018/3/9.
//  Copyright © 2018年 stoneobs@icloud.com. All rights reserved.
//

#import "NSBundle+STSystemTool.h"

@implementation NSBundle (STSystemTool)
+ (NSString *)st_applictionVersin{
    NSString * version =  [[NSBundle mainBundle] objectForInfoDictionaryKey:@"CFBundleShortVersionString"];;
    return version;
}
+  (NSString *)st_applictionBuildNum{
    NSString * build =  [[[NSBundle mainBundle] infoDictionary] objectForKey:@"CFBundleVersion"];
    return build;
}
+ (NSString *)st_applictionBundleIdentifier{
    NSString * bundleID =  [[NSBundle mainBundle] bundleIdentifier];
    return bundleID;
}
+ (NSString *)st_applictionDisplayName{
    NSString * name = [[[NSBundle mainBundle] infoDictionary] objectForKey:@"CFBundleDisplayName"];
    return name;
}
+ (NSDictionary *)st_applictionInfoPlist{
    NSDictionary * dic = [[NSBundle mainBundle] infoDictionary];
    return dic;
}
#pragma mark --文件
+ (NSString *)st_findMainBundleFilePathWithName:(NSString *)name type:(NSString *)type{
    NSString * existUrl = [[NSBundle mainBundle] pathForResource:name ofType:type];
    return existUrl;
}
+ (NSString *)st_documentDirectoryPath{
    NSString *docDir = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) firstObject];
    return docDir;
}
+ (BOOL)st_createFolderFormDocumentWithFatherPath:(NSString *)fatherPath folderName:(NSString *)folderName{
    
    NSFileManager* fileManager= [NSFileManager defaultManager];
    NSString * documentPath = [NSBundle st_documentDirectoryPath];
    if(fatherPath.length){
        NSString * realFatherPath = [NSString stringWithFormat:@"%@/%@",documentPath,fatherPath];
        BOOL isDir = NO;
        // fileExistsAtPath 判断一个文件或目录是否有效，isDirectory判断是否一个目录
        BOOL existed = [fileManager fileExistsAtPath:realFatherPath isDirectory:&isDir];
        if(existed && isDir){
             NSString * finshUrl = [NSString stringWithFormat:@"%@/%@",realFatherPath,folderName];
             [fileManager createDirectoryAtPath:finshUrl withIntermediateDirectories:YES attributes:nil error:nil];
             NSLog(@"创建目录成功:%@",finshUrl);
            return YES;
        }else{
            NSLog(@"fatherPath 不存在或者不是一个目录%@",realFatherPath);
            return NO;
        }
       
    }else{
        NSString * realPath = [NSString stringWithFormat:@"%@/%@",documentPath,folderName];
        BOOL isDir = NO;
        // fileExistsAtPath 判断一个文件或目录是否有效，isDirectory判断是否一个目录
        BOOL existed = [fileManager fileExistsAtPath:realPath isDirectory:&isDir];
        if(!existed && !isDir){
            NSString * finshUrl = realPath;
            [fileManager createDirectoryAtPath:finshUrl withIntermediateDirectories:YES attributes:nil error:nil];
            NSLog(@"创建目录成功:%@",finshUrl);
            return YES;
        }else{
            NSLog(@"%@已经存在一个目录:%@",realPath,folderName);
            return NO;
        }
    }
    return NO;
}
+ (BOOL)st_copyFile:(NSString *)file toPath:(NSString *)toPath{
    BOOL isDir = NO;
    NSFileManager* fileManager= [NSFileManager defaultManager];
        BOOL existedFile = [fileManager fileExistsAtPath:file isDirectory:&isDir];
    if(!existedFile){
         NSLog(@"该文件不存在:%@",file);
        return NO;
    }
    // fileExistsAtPath 判断一个文件或目录是否有效，isDirectory判断是否一个目录
    BOOL existed = [fileManager fileExistsAtPath:toPath isDirectory:&isDir];
    

    if(existed && isDir){
        [[NSFileManager defaultManager] copyItemAtPath:file
                                                toPath:toPath
                                                 error:nil];
        return YES;
    }else{
        NSLog(@"该位置不存在或者不是一个目录:%@",toPath);
        return NO;
    }
}
@end
